import requests
import json
import time
test_url = "https://www.zhihu.com"
def get_proxies():
    proxies = []
    for p in  requests.get("http://piping.mogumiao.com/proxy/api/get_ip_bs?appKey=734ecb2604f141bdbef7b96ad60ebeae&count=30&expiryDate=0&format=1&newLine=2").json()['msg']:
        proxies.append('%s:%s' % (p['ip'],p['port']))
    return proxies

def insert(p):
    print(p)
def valiate():
    for proxy in get_proxies():
        proxies = {
            'http': proxy,
            'https': proxy
        }
        try:
            requests.get(test_url, timeout=3, proxies=proxies, verify=False)
            insert(proxy)
        except Exception as e:
            print(e)

def run_get_proxies():

    while True:
        try:
            prx = get_proxies()
        except Exception:
            prx = []
        with open('proxies.txt','w+') as f:
            allp = f.read()
            if len(allp) > 0:
                allp = json.loads(allp)
                prx.extend(allp)
            f.write(json.dumps(prx))

run_get_proxies()